package com.sktlab.bizconfmobile.adapter;

import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.CheckBox;
import android.widget.CheckedTextView;
import android.widget.ExpandableListView;
import android.widget.ExpandableListView.OnChildClickListener;
import android.widget.ExpandableListView.OnGroupClickListener;
import android.widget.ImageView;
import android.widget.TextView;

import com.sktlab.bizconfmobile.R;
import com.sktlab.bizconfmobile.interfaces.IOnCheckedListener;
import com.sktlab.bizconfmobile.model.ContactItem;
import com.sktlab.bizconfmobile.model.Participant;
import com.sktlab.bizconfmobile.model.manager.ContactManager;
import com.sktlab.bizconfmobile.util.Util;

public class ContactListAdapter extends BaseExpandableListAdapter implements OnChildClickListener ,OnGroupClickListener{
	
	private Context mCtx;
	private LayoutInflater mInflater;
	private List<ContactItem> mContacts;
	private IOnCheckedListener mCallback;
	
	private boolean isShowEmail = false;	
	public ContactListAdapter(Context ctx, List<ContactItem> data, boolean showEmail){
		
		mCtx = ctx;
		mContacts = data;
		isShowEmail = showEmail;
		
		mInflater = (LayoutInflater)mCtx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	}
	
	@Override
	public int getGroupCount() {
		return mContacts.size();
	}

	@Override
	public int getChildrenCount(int groupPosition) {
		
		ContactItem item = mContacts.get(groupPosition);
		
		if(isShowEmail){
			
			return item.getPhone().size() + item.getEmail().size();
		}else{
			
			return item.getPhone().size();
		}
	}

	@Override
	public Object getGroup(int groupPosition) {
		
		return mContacts.get(groupPosition);
	}

	@Override
	public Object getChild(int groupPosition, int childPosition) {
		
		return mContacts.get(groupPosition).getClickedAttr(childPosition);
	}

	@Override
	public long getGroupId(int groupPosition) {
		
		return groupPosition;
	}

	@Override
	public long getChildId(int groupPosition, int childPosition) {
		
		return childPosition;
	}

	@Override
	public boolean hasStableIds() {
		
		return true;
	}

	@Override
	public View getGroupView(int groupPosition, boolean isExpanded,
			View convertView, ViewGroup parent) {
		
		GroupHolder groupHolder = null;
		
		if(convertView == null) {
			
			groupHolder = new GroupHolder();
			
			convertView = mInflater.inflate(R.layout.item_expandable_listview_group, null);
			
			groupHolder.ivGroupIndicator = (ImageView) convertView.findViewById(R.id.iv_group_indicator);
			groupHolder.tvPersonName = (TextView) convertView.findViewById(R.id.tv_person_name);
			
			convertView.setTag(groupHolder);
		}else {
			
			groupHolder = (GroupHolder)convertView.getTag();
		}
		
		if(!Util.isEmpty(groupHolder.ivGroupIndicator)){
			
			groupHolder.ivGroupIndicator.setVisibility(View.VISIBLE);
			
			//hide group indicator
			groupHolder.ivGroupIndicator.setImageResource(android.R.color.transparent);
						
//			if(isExpanded) {
//				
//				groupHolder.ivGroupIndicator.setImageResource(R.drawable.expand_group_open);
//			}else {
//				
//				groupHolder.ivGroupIndicator.setImageResource(R.drawable.expand_group_close);
//			}
		}

		groupHolder.tvPersonName.setText(mContacts.get(groupPosition).getName());
		
		return convertView;
	}

	@Override
	public View getChildView(int groupPosition, int childPosition,
			boolean isLastChild, View convertView, ViewGroup parent) {
		
		ChildHodler childHolder = null;
		
		if(convertView == null) {
			
			childHolder = new ChildHodler();
			
			convertView = mInflater.inflate(R.layout.item_expandable_listview_child, null);		
			childHolder.ctvPersonName = (CheckedTextView) convertView.findViewById(R.id.ctv_person_name);
			
			convertView.setTag(childHolder);
		}else {
			
			childHolder = (ChildHodler)convertView.getTag();
		}
			
		ContactItem item = mContacts.get(groupPosition);
		childHolder.ctvPersonName.setText(item.getClickedAttr(childPosition));
		childHolder.ctvPersonName.setChecked(item.getAttrSelectedState(childPosition));
		
		return convertView;
	}

	@Override
	public boolean isChildSelectable(int groupPosition, int childPosition) {
		
		return true;
	}
	
	public IOnCheckedListener getCallback() {
		return mCallback;
	}

	public void setCallback(IOnCheckedListener mCallback) {
		this.mCallback = mCallback;
	}

	public class GroupHolder{
		
		private ImageView ivGroupIndicator;
		private TextView tvPersonName;
	}
	
	public class ChildHodler{
		
		private TextView tvPersonName;
		private CheckBox cbCheckContact;
		private CheckedTextView ctvPersonName;
	}

	@Override
	public boolean onChildClick(ExpandableListView parent, View v,
			int groupPosition, int childPosition, long id) {
		
		CheckedTextView childCheckTextView = (CheckedTextView)v.findViewById(R.id.ctv_person_name);

		do{
			
			//Util.BIZ_CONF_DEBUG("xxx", "child clicked");
			
			if(Util.isEmpty(childCheckTextView)) {
				
				//Util.BIZ_CONF_DEBUG("xxx", "childCheckTextView is null");
				break;					
			}
			
			childCheckTextView.toggle();
			
			Participant part = new Participant();
			
			ContactItem item = mContacts.get(groupPosition);
			
			String value = item.getClickedAttr(childPosition);
			
			part.setName(item.getName());
			part.setSelectedAttrPosInContactItem(childPosition);
			part.setContactId(item.getId());
			
			switch(item.getSelectedType()) {
				
			case ContactItem.TYPE_PHONE_NUMBER:
				part.setPhone(value);
				break;
				
			case ContactItem.TYPE_EMAIL_ADDRESS:
				part.setEmail(value);
				break;
			}
			boolean isChecked = childCheckTextView.isChecked();
			
			if(isChecked) {
				
				item.setAttrSelectedState(childPosition, true);
				ContactManager.getInstance().addSelectedContact(part);
			}else {
				item.setAttrSelectedState(childPosition, false);
				ContactManager.getInstance().removeSelectedContact(part);
			}
			
			if(!Util.isEmpty(mCallback)) {
				
				mCallback.onChecked();
			}	
			
			notifyDataSetChanged();
			
		}while(false);
				
		return false;
	}

	@Override
	public boolean onGroupClick(ExpandableListView parent, View v,
			int groupPosition, long id) {

		return true;
	}
}
